/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO.InformationFilter;

import DAO.DAOAccess;
import Info.PeriodInfo;
import Info.UserInfo;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author tran tuan tai
 */
public class UsrClickDAO
{
    public void DoEmpty()
    {
        try
        {
            java.sql.Connection conn = DAOAccess.GetConnection();
            String sql = "DELETE FROM USERCLICK";
            PreparedStatement stat = conn.prepareCall(sql);
            stat.execute();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
    }

    public Integer GetNClickByUserPeriod(PeriodInfo period, UserInfo user)
    {
        Integer nClick = null;
        String query = "SELECT click from USERCLICK WHERE PERIOD = " + period.GetID() + " AND USER = " + user.id;
        try
        {
            ResultSet rs =  DAOAccess.ExecuteQuery(query);
            rs.beforeFirst();

            while(rs.next())
            {
                nClick = rs.getInt(1);
            }
            rs.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }

        return nClick;
    }

    public int Insert(int userID, int periodID, int nClick)
    {
        int id = -1;
        java.sql.Connection conn = DAOAccess.GetConnection();
        String sql = "INSERT INTO USERCLICK(USER, PERIOD, CLICK) VALUES(?, ?, ?)";
        try
        {
            PreparedStatement stat = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            stat.setInt(1, userID);
            stat.setInt(2, periodID);
            stat.setInt(3, nClick);

            stat.executeUpdate();
            ResultSet result = stat.getGeneratedKeys();
            result.first();
            id = result.getInt(1);

            stat.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return id;
    }
}
